using System;
using System.Collections.Generic;
using System.Text;

using G2007.CodeGen.Library.Generator;

namespace G2007.CodeGen.CommandLine
{
    public class main
    {

        main (string[] args)
        {
			bool compileOnly = false;
            bool verbose = false;
            if ( args.Length > 0 ) {
           
                if ( args [ 0 ] .Equals ( "-s" ) ) {
					Library.DBConvertor.DBToScript.CreateFile ( args [ 1] , args [ 2] ,
					 args [ 3] , args [ 4] , args [5] );
					 
					 return;
				}      
            }
            
			if ( args.Length == 3 )
			{
				if ( args [ 0 ].Equals ( "-c" ) || args [ 1 ].Equals ( "-c" ) ) compileOnly = true;
                if ( args[ 1 ].Equals ( "-v" ) || args [ 0 ].Equals ( "-v" ) ) verbose = true; 
                

			}

			Compiler compiler=null;

            if ( args.Length == 2 )
            {
                compiler = new Compiler ( args[ 0 ] , args[ 1 ] );
            }

			if ( args.Length == 3 )
			{

				compiler = new Compiler ( args[ 1 ] , args[ 2 ] );
			}
			
            if (args.Length == 4 )
			{
				compiler = new Compiler ( args[ 2 ] , args[ 3 ] );
			}

			compiler.Update += new Compiler.CompilerUpdate ( compiler_Update );

            if (verbose ) compiler.UpdateDetailed += new Compiler.CompilerXML ( compiler_UpdateDetailed );

			string error;

			compiler.Compile ( out error , !compileOnly, true  );

			Console.WriteLine ( error );

        }

        void compiler_UpdateDetailed ( string xml )
        {
            Console.WriteLine ( "\n\n\n\n\n\n" + xml + "\n\n\n\n\n" );
        }

		void compiler_Update ( int stage , string stageName )
		{
			Console.WriteLine ( stageName );
		}


        static int Main ( string[] args )
        {

			if ( args.Length == 0 )
			{
				Console.WriteLine ( "\nUsage: cg [-c] [-v] script app_path \n" );
				Console.WriteLine ( "Options:\n    -c                  Compile Only\n" );
                Console.WriteLine ( "Options:\n    -v                  Verbose\n" );
				return 0;
			}
			else
			{
				new main ( args );
			}




            return 0;
        }

    }
}


